/*
 * Copyright 2015-2016 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * You may not use this file except in compliance with the terms and conditions set forth
 * in the accompanying LICENSE.TXT file.
 *
 * THESE MATERIALS ARE PROVIDED ON AN "AS IS" BASIS.  AMAZON SPECIFICALLY DISCLAIMS, WITH
 * RESPECT TO THESE MATERIALS, ALL WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
 */
package com.amazon.alexa.voicechrome.internal.views.util.wave;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;

public class ThinkingProgressView extends AnimatedAmplitudeView {


    public ThinkingProgressView(Context context) {
        super(context);
    }

    public ThinkingProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ThinkingProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ThinkingProgressView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected Animator setUpAnimator() {
        // Repeating an animationSet is kind of screwy, so we'll attach our own listeners
        // Scale from half-size up to 4 (so the fading-edge gradient will scale up so no fades are visible),
        // Then fade the bar out, and call the scale image again
        final ObjectAnimator scaleAnimator = ObjectAnimator.ofFloat(amplitudeView, "scaleX", .02f, 4f);
        scaleAnimator.setDuration(700);
        scaleAnimator.setAutoCancel(false);
        scaleAnimator.setInterpolator(new LinearInterpolator());

        final ObjectAnimator fadeOut = ObjectAnimator.ofFloat(amplitudeView, "alpha", 1, 0);
        fadeOut.setDuration(200);
        fadeOut.setAutoCancel(false);

        final ObjectAnimator fadeIn = ObjectAnimator.ofFloat(amplitudeView, "alpha", 0, 1);
        fadeOut.setDuration(75);

        scaleAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                fadeIn.start();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                fadeOut.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        fadeOut.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                scaleAnimator.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        return scaleAnimator;
    }

}
